home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1996 February / macformat-034.iso / mac / Shareware City / Developers / appe Windows 2.03 / sample win.c < prev    next >
Encoding:
Text File  |  1995-12-05  |  5.2 KB  |  179 lines  |  [TEXT/CWIE]

  1. // File "sample win.c" -
  2.  
  3. #include "main.h"
  4. #include "floaters.h"
  5. #include "prefs.h"
  6. #include "sample drag.h"
  7. #include "sample win.h"
  8.  
  9. // ***********************************************************************************
  10. // Global Declarations 
  11.  
  12. extern GlobalsRec glob;
  13.  
  14. // ***********************************************************************************
  15. // ***********************************************************************************
  16.  
  17. WindowPtr NewSampleWindow() {
  18.     static short count = 0, **prefCount;
  19.     Rect bounds; 
  20.     WindowPtr win;
  21.     Str255 textBuff = "\pSample Window ", altBuff;
  22.     StringPtr winTextPtr;
  23.  
  24.     // Demo the prefs functions by incrementing the Window #
  25.     if (prefCount = (short **) GetPrefs('WCnt', 1)) {
  26.         count = (**prefCount < 999) ? ++(**prefCount) : (**prefCount = 0);
  27.         WritePrefs((Handle) prefCount, 'WCnt', 1);
  28.         DisposeHandle((Handle) prefCount);
  29.         }
  30.       else if (prefCount = (short **) NewHandle(sizeof(**prefCount))) {
  31.         **prefCount = ++count;
  32.         WritePrefs((Handle) prefCount, 'WCnt', 1);
  33.         DisposeHandle((Handle) prefCount);
  34.         }
  35.       else count++;
  36.  
  37.     // Setup the Window Title String
  38.     NumToString(count, altBuff);
  39.     BlockMove(altBuff+1, textBuff + textBuff[0] + 1, altBuff[0]);
  40.     textBuff[0] += altBuff[0];
  41.  
  42.     SetRect(&bounds, 100, 100, 320, 202);
  43.     OffsetRect(&bounds, (count-1) % 3 * 40, (count-1) % 3 * 40);
  44.     win = NewFloater(0, &bounds, textBuff, TRUE, kWDEFProcID, (WindowPtr) -1, TRUE, 0, 0,
  45.             SampleWindowEventHandler, DisposeSampleWindow);
  46.  
  47.     // Apply some text to draw for update routines
  48.     if (win) SetSampleWindowText(win,
  49. "\pAppeWin 2.03: A free demo program and source code by Matt Slot (fprefect@umich.edu) \
  50. that shows off TSM Floaters.\r\rThe latest version is free to look at or use, and can \
  51. be downloaded from your local Sumex or Mac-Archives mirror site.");
  52.         
  53.     if (win) {
  54.         UpdateFloater(win);    // Force an immediate draw
  55.         if (glob.hasDragMgr) SetupDragHandlers(win);
  56.         }
  57.     
  58.     return(win);
  59.     }
  60.  
  61. // ***********************************************************************************
  62. // ***********************************************************************************
  63.  
  64. void DisposeSampleWindow(WindowPtr win) {
  65.     StringPtr winTextPtr;
  66.     
  67.     if (winTextPtr = (StringPtr) GetWRefCon(win)) DisposePtr((Ptr) winTextPtr);
  68.     DisposeFloater(win);
  69.     
  70.     // For a background only app, this will cause us to quit when the user
  71.     //   closes the last floater. Its your choice if you want this to happen.
  72.     if (glob.bkgdOnly && ! GetIndFloater(1, FALSE)) glob.quitting = TRUE;
  73.     }
  74.  
  75. // ***********************************************************************************
  76. // ***********************************************************************************
  77.  
  78. void SampleWindowEventHandler(EventRecord *theEvent, WindowPtr win) {
  79.     Rect destRect;
  80.     GrafPtr savePort;
  81.     StringPtr winTextPtr;
  82.  
  83.     switch(theEvent->what) {
  84.         case nullEvent:
  85.             break;
  86.         case mouseDown: {
  87.             // The Window part has already been found and forwarded to us
  88.             short thePart = theEvent->message;    
  89.             
  90.             switch(thePart) {
  91.                 case inContent:
  92.                     GetPort(&savePort);
  93.                     SetPort(win);
  94.  
  95.                     if (glob.hasColorQD)
  96.                         LMSetHiliteMode(LMGetHiliteMode() ^ (1L << hiliteBit));
  97.                     InvertRect(&win->portRect);
  98.  
  99.                     if (glob.hasDragMgr) SampleWindowDrag(theEvent, win);
  100.                       else while(StillDown());
  101.  
  102.                     if (glob.hasColorQD)
  103.                         LMSetHiliteMode(LMGetHiliteMode() ^ (1L << hiliteBit));
  104.                     InvertRect(&win->portRect);
  105.                     
  106.                     SetPort(savePort);
  107.                     break;
  108.                 case inGrow:
  109.                     break;
  110.                 case inGoAway:
  111.                     if (TrackGoAway(win, theEvent->where))
  112.                         DisposeSampleWindow(win);
  113.                     break;
  114.                 case inZoomIn:
  115.                     break;
  116.                 case inZoomOut:
  117.                     break;
  118.                 }
  119.             break;
  120.             }
  121.         case keyDown:
  122.         case autoKey:
  123.             break;
  124.         case updateEvt: {
  125.             GetPort(&savePort);
  126.             SetPort(win);
  127.             BeginUpdate(win);
  128.             
  129.             // Draw something cool!
  130.             destRect = win->portRect;
  131.             EraseRect(&destRect);
  132.             InsetRect(&destRect, 3, 2);
  133.             TextFont(1); TextSize(9);
  134.             if (winTextPtr = GetSampleWindowText(win)) 
  135.                 TextBox(winTextPtr+1, winTextPtr[0], &destRect, teFlushDefault);
  136.             TextFont(0); TextSize(0);
  137.             
  138.             EndUpdate(win);
  139.             SetPort(savePort);
  140.             break;
  141.             }
  142.         }
  143.     }
  144.  
  145. // ***********************************************************************************
  146. // ***********************************************************************************
  147.  
  148. void SetSampleWindowText(WindowPtr win, StringPtr winText) {
  149.     Boolean dirty = FALSE;
  150.     Ptr storage = (Ptr) GetWRefCon(win);
  151.     GrafPtr savePort;
  152.     
  153.     if (! storage) { storage = NewPtr(sizeof(Str255)); dirty = TRUE; }
  154.     if (! storage) return;
  155.     
  156.     if (winText && ! EqualString(winText, (StringPtr) storage, 0, 0)) dirty = TRUE;
  157.     BlockMove((winText && winText[0]) ? winText : "\p", storage, sizeof(Str255));
  158.     SetWRefCon(win, (long) storage);
  159.     
  160.     // Force a redraw after changing content
  161.     if (dirty) {
  162.         GetPort(&savePort);
  163.         SetPort(win);
  164.         InvalRect(&win->portRect);
  165.         SetPort(savePort);
  166.         }
  167.     }
  168.  
  169. // ***********************************************************************************
  170. // ***********************************************************************************
  171.  
  172. StringPtr GetSampleWindowText(WindowPtr win) {
  173.     Ptr storage = (Ptr) GetWRefCon(win);
  174.     StringPtr winText;
  175.  
  176.     winText = (storage && ((StringPtr) storage[0])) ? ((StringPtr) storage) : "\p<No Data>";
  177.     return(winText); 
  178.     }
  179.